# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

class FT_8082:
    result=[]
    analysisDecription = ['自定义消息体长度：', '应答流水号：','上报类别：', '结果：', '验证差异属性总数：','验证差异属性项列表']
    analysis8082Length=[2,2,1,1,1]

    attribute_list = ['属性ID：', '属性长度：', '属性值：']
    atb_list_len = [1, 1, 0]
    terminal_attributes = [
        {'id': '1', 'des': '车辆识别码（VIN）：', 'remark': '读写'},
        {'id': '2', 'des': '通讯编码（终端ID）：', 'remark': '只读'},  # 默认7字节
        {'id': '3', 'des': '贴片SIM卡号码：', 'remark': '只读'},  # 默认7字节
        {'id': '4', 'des': '设备编号：', 'remark': '只读'},  # 默认13字节
        {'id': '5', 'des': '设备追溯号：', 'remark': '只读'},  # 默认26字节
        {'id': '6', 'des': '图样代号：', 'remark': '只读'},  # 默认13字节
        {'id': '7', 'des': '可插拔SIM卡SIM卡串号（ICCID）：', 'remark': '只读'},
        {'id': '8', 'des': '可插拔SIM卡号码：', 'remark': '读写'},
        {'id': '9', 'des': '终端型号：', 'remark': '只读'},
        {'id': '10', 'des': '终端硬件版本号格式：', 'remark': '只读'},
        {'id': '11', 'des': '终端固件版本号格式：', 'remark': '只读'},
        {'id': '12', 'des': '车牌号码：', 'remark': '读写'},
        {'id': '13', 'des': '车牌颜色：', 'remark': '读写'},
        {'id': '14', 'des': '车牌分类：', 'remark': '读写'},
        {'id': '15', 'des': '发动机类型：', 'remark': '读写'},
        {'id': '16', 'des': '福田通道主IP：', 'remark': '读写'},
        {'id': '17', 'des': '福田通道备IP：', 'remark': '读写'},
        {'id': '18', 'des': '福田通道端口序号：', 'remark': '读写'},
        {'id': '19', 'des': '贴片卡SIM卡串号：', 'remark': '只读'},
        {'id': '20', 'des': '开始锁车时间：', 'remark': '废止'},
        {'id': '21', 'des': '绑定转速：', 'remark': '只读'},
        {'id': '22', 'des': '锁定转速：', 'remark': '只读'},
        {'id': '23', 'des': 'ECU绑定标识：', 'remark': '只读'},
        {'id': '24', 'des': '锁车标识：', 'remark': '只读'},
        {'id': '25', 'des': '发动机编号：', 'remark': '只读'},
        {'id': '26', 'des': 'ECU绑定密码：', 'remark': '只读'},
        {'id': '27', 'des': 'ECU解绑密码：', 'remark': '只读'},
        {'id': '28', 'des': 'ECU固定密钥：', 'remark': '只读'},
        {'id': '29', 'des': '中断至锁车时间间隔小时数：', 'remark': '废止'},
        {'id': '30', 'des': '贷款模式：', 'remark': '读写'},
        {'id': '31', 'des': '开始锁车时间提示天数：', 'remark': '废止'},
        {'id': '32', 'des': '终端密钥：', 'remark': '只读'},
        {'id': '33', 'des': '终端锁车ID：', 'remark': '只读'},
        {'id': '34', 'des': '车型信息：', 'remark': '只读'},
        {'id': '35', 'des': '发动机额定功率：', 'remark': '只读'}
    ]
    atb_total=0
    def LogicAisle(self, protocolStr, header):
        s=''
        s=protocolStr[0:4]
        header.append(self.analysisDecription[0]+s+'\n')
        s=protocolStr[4:8]
        header.append(self.analysisDecription[1]+s+'\n')
        s=protocolStr[8:10]
        msg=''
        if int(s,16)==1:
            msg="双通道终端绑定车辆识别码（VIN）"
        elif int(s,16)==2:
            msg="双通道SIM卡更换"
        elif int(s, 16) == 3:
            msg = "单通道终端绑定车辆识别码"
        elif int(s, 16) == 4:
            msg = "单通道SIM卡更换"
        elif int(s, 16) == 100:
            msg = "终端属性变化"
        header.append(self.analysisDecription[2]+s+ msg + '\n')
        s=protocolStr[10:12]
        result=int(s,16)
        if result == 0:
            msg = "成功"
        elif result == 1:
            msg = "消息有误"
        elif result == 2:
            msg = "缺少必要属性项"
        elif result == 3:
            msg = "终端已与相同VIN绑定"
        elif result == 4:
            msg = "不符"
        else:
            msg = ''
        header.append(self.analysisDecription[3] + s + msg + '\n')
        if result==4:
            s=protocolStr[12:14]
            self.atb_total=int(s,16)
            header.append(self.analysisDecription[4]+s+'\n')
            s=protocolStr[14:]
            for i in range(self.atb_total):
                atb_id = s[:2]
                atb_len = int(s[2:4], 16)
                self.atb_list_len[2] = atb_len
                atb_des = ''
                atb_remark = ''
                for atb in self.terminal_attributes:
                    if int(atb['id']) == int(atb_id, 16):
                        atb_des = atb['des']
                        atb_remark = atb['remark']
                        break
                    else:
                        pass
                atb_val = s[4:4 + atb_len * 2]
                s = s[4 + atb_len * 2:]
                header.append(self.attribute_list[0] + atb_id + '\n')
                header.append(self.attribute_list[1] + str(atb_len) + '\n')
                atb_id = int(atb_id, 16)
                if atb_id == 1 or atb_id == 2 or atb_id == 4 or atb_id == 5 or atb_id == 6 or atb_id == 7 or atb_id == 8 or atb_id == 9 or atb_id == 10 or atb_id == 11 \
                        or atb_id == 12 or atb_id == 16 or atb_id == 17 or atb_id == 19:
                    atb_val = atb_val.decode("hex").decode('gb2312')
                    header.append(self.attribute_list[2] + atb_des + '' + atb_val + '' + atb_remark + '\n')
                elif atb_id == 13 or atb_id == 31:
                    atb_val = int(atb_val, 16)
                    header.append(self.attribute_list[2] + atb_des + '' + str(atb_val) + '' + atb_remark + '\n')
                elif atb_id == 14:
                    msg = ''
                    atb_val = int(atb_val, 16)
                    if atb_val == 1:
                        msg = '：大型汽车'
                    elif atb_val == 2:
                        msg = '：使馆汽车'
                    elif atb_val == 3:
                        msg = '：领馆汽车'
                    elif atb_val == 4:
                        msg = '：港澳入出境车'
                    elif atb_val == 5:
                        msg = '：教练汽车 '
                    elif atb_val == 6:
                        msg = '：警用汽车'
                    elif atb_val == 7:
                        msg = '：挂车'
                    elif atb_val == 8:
                        msg = '：低速车'
                    elif atb_val == 9:
                        msg = '：临时行驶车'
                    elif atb_val == 10:
                        msg = '：临时入境车'
                    elif atb_val == 11:
                        msg = '：小型汽车'
                    header.append(self.attribute_list[2] + atb_des + '' + str(atb_val) + msg + atb_remark + '\n')
                elif atb_id == 18:
                    msg = ''
                    atb_val = int(atb_val, 16)
                    if atb_val == 1:
                        msg = '：欧曼'
                    elif atb_val == 2:
                        msg = '：奥铃'
                    elif atb_val == 3:
                        msg = '：伽途'
                    elif atb_val == 4:
                        msg = '：时代'
                    elif atb_val == 5:
                        msg = '：萨瓦纳 '
                    elif atb_val == 6:
                        msg = '：拓路者'
                    elif atb_val == 7:
                        msg = '：欧马可'
                    elif atb_val == 8:
                        msg = '：雷萨'
                    elif atb_val == 0:
                        msg = '：默认端口'
                    header.append(self.attribute_list[2] + atb_des + '' + str(atb_val) + msg + atb_remark + '\n')
                elif atb_id == 30:
                    msg = ''
                    if atb_val == "00":
                        msg = '：未知'
                    elif atb_val == "01":
                        msg = '：非贷款车'
                    elif atb_val == "02":
                        msg = '：福田自营贷款车'
                    elif atb_val == "03":
                        msg = '：非自营贷款车'
                    header.append(self.attribute_list[2] + atb_des + '' + atb_val + msg + atb_remark + '\n')
                else:
                    header.append(self.attribute_list[2] + atb_des + '' + atb_val + ' \n')
        else :
            pass

        self.result = header

